# geometry.twoD.collections -- Module of collection classes for geometry objects
#
# Vic Kelson, 18 February 2000
#

import copy
import misc.collections

BIG_REAL = 1.0e30

class SearchableList(misc.collections.CircularList):
	"""
	Implements a collection of items that are currently to be edited.
	The constructor recieves a list of items to be considered.

	All items in the list must implement the method distance2(self,pt),
	which returns the square of the distance from the item to pt.

	Methods:

	select(self,pt,tol2=1.0e30):

	Selects the element in the queue that is nearest to the point 'pt',
	starting from the "next" item in the list.  If the square of the
	distance from 'pt' to the nearest object is larger than 'tol2',
	returns None.
	
	selectBox(self,box):
	
	Returns a list of elements from the queue that cross the bounding
	box 'bbox'. Returns an empty list if no features are found.
	"""
	
	def select(self,pt,tol=1.0e30):
		tol2 = tol*tol
		self.next()
		if len(self.list) > 0:
			startItem = self.item
			d2 = []
			for e in self.list:
				d2.append(e.distance2(pt))
			i = d2.index(min(d2))
			if d2[i] < tol2:
				# Aha!  We found it.  Make the list point to the object.
				self.list = self.list[i:] + self.list[:i]
				return self.list[0]
			else:
				return None
		else:
			return None
		
	def selectBox(self,bbox):
		raise "Not yet implemented"
